################################################################################
#
#                       美国民粹主义外交政策的民意基础                       
#
#                            付 舒    2025年10月     
#
################################################################################


#####（一）数据  ######

library(tidyverse)
library(haven)

# set working directory
setwd("/Users/shufu/Desktop/Fu2025_PopulistForeignPolicy_Replication/")

# ANES 2020 
anes2020_raw <- read_dta("anes_timeseries_2020.dta")

anes2020 <- anes2020_raw %>%
  dplyr::select(
    # id
    id = V200001,
    
    # meta
    full_weights = V200010b,
    
    # demographics
    gender = V201600,
    age = V201507x,
    income = V202468x,
    pid3 = V201228,
    pid7 = V201231x,
    race = V201549x,  
    educ = V201510,   
    
    # presidential affect
    bidenft = V201151,
    trumpft = V201152,
    
    # vote choice
    post_pres_vote = V202110x, # 1-5,7,9; 1-2 are important
  
    # POLITICAL POPULISM
    gov_trust = V201233,             # 1-5, always to never
    polit_num_corrupt = V201236,     # 1-5, all to none
    wide_corruption = V202425,       # 1-4 widespread to not at all
    polit_no_care = V202410,         # 1-5, agree to disagree
    polit_trust = V202411,           # 1-5, agree to disagree
    polit_problem = V202412,         # 1-5, agree to disagree 
    polit_interest_rich = V202415,   # 1-5, agree to disagree
    polit_no_say = V202213,          # 1-5, agree to disagree
    
    # ECONOMIC POPULISM
    immig_take_job = V202233,  # 1-4, likely to unlikely
    finance_worry = V201594,   # 1-5, worried to not worried
    immig_good_econ = V202418, # 1-5, agree to disagree
    trade_increase = V202362,  # 1 good; 2 bad; 3 neither good nor bad
    free_trade = V202359,      # 1 favor; 2 oppose; 3 neither favor nor oppose
    free_trade_str = V202360,  # 1 a great deal; 2 moderately; 3 a little
    econ_worry = V201335,      # 1 not at all; 5 extremely worried
    econ_status = V202430,     # 1 much better; 5 much worse
    national_econ = V201327x,  # 1 much better; 5 much worse
    unemployment = V201333x,   # 1 much better; 5 much worse
    
    # CULTURAL POPULISM
    immig_policy = V201417,    # 1-4, felons to allow to stay
    syrian_refug = V202236x,   # 1-7, allow to oppose ### NOT ONLY FOR SYRIAN
    immig_restrict = V202232,  # 1-5, increase immigration to decrease immigration
    america_customs = V202424, # 1-4, important to not important
    muslimft = V202168,        # 0-100
    illeg_immig_ft = V202481,  # 0-100
    govt_help_black = V201258, # 1-7, help to not help
    mexico_wall = V201426x,    # 1-7, favor to oppose
    
    # CHINA RELATED
    chn_threat = V202400,
    rus_threat = V202401,
    mex_threat = V202402,
    irn_threat = V202403,
    jpn_threat = V202404,
    ger_threat = V202405,
    
    # FOREIGN ISSUES
    worry_terrorist = V202358,           # 1 extremely worried 5 not at all worried
    free_trade7 = V202361x,              # 1 favor a great deal -- 7 oppose a great deal
    approval_trump_foreign = V201135x,   # 1 approve strongly -- 4 disapprove strongly
    defense_spending = V201249,          # 1 greatly decrease ; 7 greatly increase
    us_role = V201346,                   # 1 weaker; 2 stayed about the same; 3 stronger
    isolate = V201349x,                  # 1 agree strongly -- 4 disagree strongly
    military_use = V201350,               # 1 extremely willing -- 5 not at all willing
    
    # COMMON VARIABLES
    cv_vote2020 = V202110x,
    cv_ideology = V201200,
    cv_party = V201228,
    cv_religion = V201435,
    cv_house = V201582,
    cv_gender = V201600,
    cv_age = V201507x,
    cv_marital = V201508,
    cv_race = V201549x,
    cv_education = V201510,
    cv_income = V202468x,
    
  ) %>%
  
  filter(pid7 %in% 1:7 & pid3 %in% 1:3) %>%
  filter(gender %in% 1:2 & age %in% 18:90 & income %in% 1:22) %>%
  filter(race %in% 1:6 & educ %in% 1:8) %>%
  
  filter(bidenft %in% 0:100 & trumpft %in% 0:100) %>%
  
  filter(immig_policy %in% 1:4) %>%
  filter(syrian_refug %in% 1:7) %>%
  filter(immig_restrict %in% 1:5) %>%
  filter(america_customs %in% 1:4) %>%
  filter(muslimft %in% 0:100) %>%
  filter(illeg_immig_ft %in% 0:100) %>%
  filter(govt_help_black %in% 1:7) %>%
  filter(mexico_wall %in% 1:7) %>%
  
  filter(immig_take_job %in% 1:4) %>%
  filter(immig_good_econ %in% 1:5) %>%
  filter(finance_worry %in% 1:5) %>%
  filter(trade_increase %in% 1:3) %>%
  filter(free_trade %in% 1:3) %>%
  filter(free_trade_str %in% 1:3) %>%
  filter(econ_worry %in% c(1:5)) %>%
  filter(econ_status %in% c(1:5)) %>%
  filter(national_econ %in% c(1:5)) %>%
  filter(unemployment %in% c(1:5)) %>%
  
  filter(gov_trust %in% 1:5) %>%
  filter(polit_num_corrupt %in% 1:5) %>%
  filter(wide_corruption %in% 1:4) %>%
  filter(polit_no_care %in% 1:5) %>%
  filter(polit_trust %in% 1:5) %>%
  filter(polit_problem %in% 1:5) %>%
  filter(polit_interest_rich %in% 1:5) %>%
  filter(polit_no_say %in% 1:5) %>%
  
  mutate(
    pid3 = case_when(pid3 == 1 ~ 0, 
                     pid3 == 3 ~ 0.5, 
                     pid3 == 2 ~ 1),
    pid7 = (pid7 - 1) / 6,
    isfemale = as.numeric(gender == 2),
    iswhite = as.numeric(race == 1),
    post_pres_vote = post_pres_vote,
    
    trumpft = trumpft,
    bidenft = bidenft,
    
    immig_policy = (immig_policy - 2.5) / -1.5,
    syrian_refug = (syrian_refug - 4) / 3,
    immig_restrict = (immig_restrict - 3) / 2,
    america_customs = (america_customs - 2.5) / -1.5,
    muslimft = (muslimft - 50) / -50,
    illeg_immig_ft = (illeg_immig_ft - 50) / -50,
    govt_help_black = (govt_help_black - 4) / 3,
    mexico_wall = (mexico_wall - 4) / -3,
    
    immig_take_job = (immig_take_job - 2.5) / -1.5,
    finance_worry = (finance_worry - 3) / -2,
    immig_good_econ = (immig_good_econ - 3) / 2,
    trade_increase = case_when(
      trade_increase == 1 ~ -1,
      trade_increase == 3 ~ 0,
      trade_increase == 2 ~ 1
    ),
    free_trade7 = case_when(
      free_trade == 1 & free_trade_str == 1 ~ -1,
      free_trade == 1 & free_trade_str == 2 ~ -2/3, 
      free_trade == 1 & free_trade_str == 3 ~ -1/3, 
      free_trade == 3                       ~  0,
      free_trade == 2 & free_trade_str == 3 ~  1/3,
      free_trade == 2 & free_trade_str == 2 ~  2/3,
      free_trade == 2 & free_trade_str == 1 ~  1
    ),
    free_trade3 = case_when(
      free_trade == 1 ~ -1,
      free_trade == 2 ~ 1,
      free_trade == 3 ~ 0
    ),
    econ_worry = (econ_worry - 3) / 2,
    econ_status = (econ_status - 3) / 2,
    national_econ = (national_econ - 3) / 2,
    unemployment = (unemployment - 3) / 2,
    
    gov_trust = (gov_trust - 3) / 2,
    polit_num_corrupt = (polit_num_corrupt - 3) / -2,
    wide_corruption = (wide_corruption - 2.5) / -1.5,
    polit_no_care = (polit_no_care - 3) / -2,
    polit_trust = (polit_trust - 3) / 2,
    polit_problem = (polit_problem - 3) / -2,
    polit_interest_rich = (polit_interest_rich - 3) / -2,
    polit_no_say = (polit_no_say - 3) / -2,
    
    chn_threat = ifelse(chn_threat %in% 1:5, (chn_threat - 1) / 4, NA),
    rus_threat = ifelse(rus_threat %in% 1:5, (rus_threat - 1) / 4, NA),
    mex_threat = ifelse(mex_threat %in% 1:5, (mex_threat - 1) / 4, NA),
    irn_threat = ifelse(irn_threat %in% 1:5, (irn_threat - 1) / 4, NA),
    jpn_threat = ifelse(jpn_threat %in% 1:5, (jpn_threat - 1) / 4, NA),
    ger_threat = ifelse(ger_threat %in% 1:5, (ger_threat - 1) / 4, NA),
    
    worry_terrorist =        ifelse(worry_terrorist %in% c(1:5), - (worry_terrorist - 5) / 4, NA),
    approval_trump_foreign = ifelse(approval_trump_foreign %in% c(1:4), - (approval_trump_foreign - 4) / 3, NA),
    defense_spending =       ifelse(defense_spending %in% c(1:7), (defense_spending - 1) / 6, NA),
    us_role =                ifelse(us_role %in% c(1:3), (us_role - 1) / 2, NA),
    isolate =                ifelse(isolate %in% c(1:4), - (isolate - 4) / 3, NA),
    military_use =           ifelse(military_use %in% c(1:5), - (military_use - 5) / 4, NA),
    
    cv_vote2020trump = ifelse(cv_vote2020 == 2, 1, 0),
    cv_ideology = ifelse(cv_ideology %in% c(1:7), (cv_ideology - 1) / 6, NA),
    cv_party = ifelse(cv_party %in% c(1:3),
                      case_when(cv_party == 1 ~ 0, 
                                cv_party == 3 ~ 0.5, 
                                cv_party == 2 ~ 1), NA),
    cv_religion_catho = ifelse(cv_religion == 2, 1, 0),
    cv_religion_chris = ifelse(cv_religion == 1, 1, 0),
    cv_religion_other = ifelse(cv_religion %in% c(3:8), 1, 0),
    cv_religion_no = ifelse(cv_religion %in% c(-9, 9:12), 1, 0),
    cv_houseowner = ifelse(cv_house == 1, 1, 0),
    cv_female = ifelse(cv_gender == 2, 1, 0),
    cv_age = ifelse(cv_age == -9, NA, cv_age),
    cv_marital_mar = ifelse(cv_marital %in% c(1, 2), 1, 0),
    cv_marital_wds = ifelse(cv_marital %in% c(3:5), 1, 0),
    cv_marital_nm = ifelse(cv_marital %in% c(-9, 6), 1, 0),
    cv_race_white = ifelse(cv_race == 1, 1, 0),
    cv_race_black = ifelse(cv_race == 2, 1, 0),
    cv_race_latin = ifelse(cv_race == 3, 1, 0),
    cv_race_other = ifelse(cv_race != 1 & cv_race != 2 & cv_race != 3, 1, 0),
    cv_education = case_when(cv_education == 1 ~ 1,
                             cv_education == 2 ~ 2,
                             cv_education %in% c(3:5) ~ 3,
                             cv_education == 6 ~ 4,
                             cv_education %in% c(7:8) ~ 5,
                        ),
    cv_income = case_when(cv_income == 1 ~ 1,
                          cv_income %in% c(2:4) ~ 2,
                          cv_income %in% c(5:9) ~ 3,
                          cv_income %in% c(10:13) ~ 4,
                          cv_income %in% c(14:16) ~ 5,
                          cv_income %in% c(17:19) ~ 6,
                          cv_income %in% c(20:22) ~ 7)
  )



# CCGA 2022 
ccga2022_raw <- read_dta("/Users/shufu/Desktop/populism2.0/data/ccga2022.dta")


ccga2022 <- ccga2022_raw %>%
  mutate(

    # COMMON VARIABLES
    cv_vote2020 = Q2020VOTE,
    cv_ideology = Q1005,
    cv_party = Q1010,
    cv_religion = DOV_REL1,
    cv_house = pprent,
    cv_children = ppkid017,
    cv_gender = ppgender,
    cv_age = ppage,
    cv_marital = ppmarit5,
    cv_race = ppethm,
    cv_education = ppeduc5,
    cv_income = ppinc7,
    
    # Overall Foreign Affairs Attitudes
    prioriy = Q68,
    threat_immi = Q5_NEW_6a,
    threat_econ = Q5_NEW_28b,
    threat_demo = Q5_NEW_38,
    threat_china = Q5_NEW_45,
    legal_immi = Q390,
    
    # China
    threat_china = Q5_NEW_45,
    coor_china_rise = Q808_7,
    invade_taiwan = Q30_2,
    ft_china = Q45_6,
    ft_taiwan = Q45_24,
    
    # feeling thermometer
    ft_russia = Q45_12,
    ft_israel = Q45_4,
    ft_india = Q45_28,
    ft_uk = Q45_5,
    
    # isolationist
    isolate = Q3,
    cost = Q13,
    threat_within = Q807,
    no_involve = Q7D_3,
    part_io = Q8_15,
    alliance = Q8_5,
    nato = Q200,
    iran_deal = qiran,
    
    # anti-globalist 
    tariff = Q8_16,
    econ_super = Q8_17,
    econ_aid = Q8_7,
    sanction = Q8_3,
    
    # anti-values
    prom_hr_demo = Q808_8,
    china_min = Q7D_20,
    demo_best = Q69,
    dei = Q391,

    
    
  ) %>%
  mutate(
    
    isolate = case_when(isolate == 2 ~ 1,
                        isolate == 1 ~ 0,
                        isolate == -1 ~ NA),
    cost = case_when(cost == 1 ~ 1,
                     cost == 2 ~ 0,
                     cost == -1 ~ NA),
    threat_within = case_when(threat_within == 1 ~ 1,
                              threat_within == 2 ~ 0,
                              threat_within == -1 ~ NA),
    alliance = case_when(alliance == 1 ~ 0,
                         alliance == 2 ~ 1/3,
                         alliance == 3 ~ 2/3,
                         alliance == 4 ~ 1,
                         alliance == -1 ~ NA),
    no_involve = case_when(no_involve == 1 ~ 1,
                           no_involve == 2 ~ 2/3,
                           no_involve == 3 ~ 1/3,
                           no_involve == 4 ~ 0,
                           no_involve == -1 ~ NA),
    part_io = case_when(part_io == 1 ~ 0,
                        part_io == 2 ~ 1/3,
                        part_io == 3 ~ 2/3,
                        part_io == 4 ~ 1,
                        part_io == -1 ~ NA),
    nato = case_when(nato == 1 ~ 0,
                     nato == 2 ~ 1/3,
                     nato == 3 ~ 2/3,
                     nato == 4 ~ 1,
                     nato == -1 ~ NA),
    
    tariff = case_when(tariff == 1 ~ 1,
                       tariff == 2 ~ 2/3,
                       tariff == 3 ~ 1/3,
                       tariff == 4 ~ 0,
                       tariff == -1 ~ NA),
    econ_super = case_when(econ_super == 1 ~ 1,
                           econ_super == 2 ~ 2/3,
                           econ_super == 3 ~ 1/3,
                           econ_super == 4 ~ 0,
                           econ_super == -1 ~ NA),
    econ_aid = case_when(econ_aid == 1 ~ 0,
                         econ_aid == 2 ~ 1/3,
                         econ_aid == 3 ~ 2/3,
                         econ_aid == 4 ~ 1,
                         econ_aid == -1 ~ NA),
    sanction = case_when(sanction == 1 ~ 1,
                         sanction == 2 ~ 2/3,
                         sanction == 3 ~ 1/3,
                         sanction == 4 ~ 0,
                         sanction == -1 ~ NA),
    threat_econ = case_when(threat_econ == 1 ~ 1,
                            threat_econ == 2 ~ 0.5,
                            threat_econ == 3 ~ 0,
                            threat_econ == -1 ~ NA),
    
    anti_hr_demo = case_when(prom_hr_demo == 1 ~ 0,
                             prom_hr_demo == 2 ~ 0.5,
                             prom_hr_demo == 3 ~ 1,
                             prom_hr_demo == -1 ~ NA),
    demo_not_best = case_when(demo_best == 1 ~ 0,
                              demo_best == 2 ~ 1,
                              demo_best == -1 ~ NA),
    anti_dei = case_when(dei == 1 ~ 0,
                         dei == 2 ~ 1/4,
                         dei == 3 ~ 2/4,
                         dei == 4 ~ 3/4,
                         dei == 5 ~ 1,
                         dei == -1 ~ NA),
    threat_demo = case_when(threat_demo == 1 ~ 0,
                            threat_demo == 2 ~ 0.5,
                            threat_demo == 3 ~ 1,
                            threat_demo == -1 ~ NA),
    
    cv_vote2020trump = ifelse(cv_vote2020 == 2, 1, 0),
    cv_ideology = ifelse(cv_ideology %in% c(1:7), (cv_ideology - 1) / 6, NA),
    cv_party = ifelse(cv_party %in% c(1:3),
                      case_when(cv_party == 2 ~ 0, 
                                cv_party == 3 ~ 0.5, 
                                cv_party == 1 ~ 1), NA),
    cv_religion_catho = ifelse(cv_religion == 1, 1, 0),
    cv_religion_chris = ifelse(cv_religion %in% c(2, 11), 1, 0),
    cv_religion_other = ifelse(cv_religion %in% c(3:10, 12), 1, 0),
    cv_religion_no = ifelse(cv_religion == 13, 1, 0),
    cv_houseowner = ifelse(cv_house == 1, 1, 0),
    cv_female = ifelse(cv_gender == 2, 1, 0),
    cv_age = ifelse(cv_age >= 80, 80, cv_age),
    cv_marital_mar = ifelse(cv_marital %in% c(1), 1, 0),
    cv_marital_wds = ifelse(cv_marital %in% c(2:4), 1, 0),
    cv_marital_nm = ifelse(cv_marital %in% c(5), 1, 0),
    cv_race_white = ifelse(cv_race == 1, 1, 0),
    cv_race_black = ifelse(cv_race == 2, 1, 0),
    cv_race_latin = ifelse(cv_race == 4, 1, 0),
    cv_race_other = ifelse(cv_race != 1 & cv_race != 2 & cv_race != 4, 1, 0),
    cv_education = cv_education,
    cv_income = cv_income,
    
    ft_china = ifelse(ft_china == -1, NA, ft_china),
    ft_taiwan = ifelse(ft_taiwan == -1, NA, ft_taiwan),
    ft_russia = ifelse(ft_russia == -1, NA, ft_russia),
    ft_israel = ifelse(ft_israel == -1, NA, ft_israel),
    ft_india = ifelse(ft_india == -1, NA, ft_india),
    ft_uk = ifelse(ft_uk == -1, NA, ft_uk),
    
    coor_china_rise = case_when(coor_china_rise == 1 ~ 1,
                                coor_china_rise == 2 ~ 0.5,
                                coor_china_rise == 3 ~ 0,
                                coor_china_rise == -1 ~ NA),
    
    priority_inter_coop = ifelse(prioriy == 3, 1, 0),
    priority_econ_gain = ifelse(prioriy == 4, 1, 0),
    priority_demo_value = ifelse(prioriy == 5, 1, 0),
    
    econ_not_priority = ifelse(prioriy != 4, 1, 0),
    
    threat_china = case_when(threat_china == 1 ~ 1,
                             threat_china == 2 ~ 0.5,
                             threat_china == 3 ~ 0,
                             threat_china == -1 ~ NA),
    coor_china_rise = case_when(coor_china_rise == 1 ~ 1,
                                coor_china_rise == 2 ~ 0.5,
                                coor_china_rise == 3 ~ 0,
                                coor_china_rise == -1 ~ NA),
    invade_taiwan = case_when(invade_taiwan == 1 ~ 1,
                              invade_taiwan == 2 ~ 0,
                              invade_taiwan == -1 ~ NA),
    ft_china = ifelse(ft_china == -1, NA, ft_china),
    ft_taiwan =ifelse(ft_taiwan == -1, NA, ft_taiwan)
    
  ) 

